package com.kfm.dbutils.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * SpringBoot 的配置类
 */
@Configuration // 声明当前类是一个配置类，相当于一个Spring配置的xml文件
public class BeanConfig {

    @Value("${my.db.driver}")
    private String driver;

    @Value("${my.db.url}")
    private String url;

    @Value("${my.db.username}")
    private String username;

    @Value("${my.db.password}")
    private String password;


    @Bean// 声明当前方法的返回值是一个Bean对象，会被装配到Spring容器中
    public QueryRunner queryRunner(DataSource dataSource){
        return new QueryRunner(dataSource);
    }

    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        return dataSource;
    }
}
